package demo.mqtt;

import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author wangly on @date 2024/4/8
 */
public class testC111 {

    static String instanceId = MqttConfig.instanceId;
    static String serverUri = MqttConfig.NAMESRV_ADDR;
    static String accessKey = MqttConfig.AccessKey;
    static String secretKey = MqttConfig.SecretKey;
    static String deviceId = MqttConfig.deviceId;
    static String clientId = MqttConfig.clientId;

    final static  String parentTopic = MqttConfig.parentTopic;

    public static void main(String[] args) throws Exception{
        final MemoryPersistence memoryPersistence = new MemoryPersistence();
        MqttClient connection = new MqttClient(serverUri, clientId, memoryPersistence);
            final ExecutorService executorService = new ThreadPoolExecutor(1, 1, 0, TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue<Runnable>());
            connection.setCallback(new MqttCallbackExtended() {
                @Override
                public void connectionLost(Throwable cause) {

                }

                @Override
                public void messageArrived(String topic, MqttMessage message) throws Exception {

                }

                @Override
                public void deliveryComplete(IMqttDeliveryToken token) {

                }

                @Override
               public void connectComplete(boolean reconnect, String serverURI) {
                   /**
                    * 客户端连接成功后就需要尽快订阅需要的 topic
                    */
                   System.out.println("connect success");
                   executorService.submit(new Runnable() {
                       @Override
                       public void run() {
//                           try {
////                               mqttClient.subscribe(topic, qosLevel);
//                               System.out.println("topic success");
//                           } catch (MqttException e) {
//                               e.printStackTrace();
//                           }
                       }
                   });
               }
           });

    }

}
